package com.liantong.demo_part2.Controller;

import com.liantong.demo_part2.Service.RankService;
import com.liantong.demo_part2.Utils.JsonResult;
import com.liantong.demo_part2.Utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Api(tags = "优先度评估的接口")
@Controller
@RequestMapping("/Access/Rank")
public class RankController {

    @Autowired
    private RankService rankService;

    private Result result = new Result();


    /**
     * 直接打分法
     */
    @RequestMapping(value = "/zhijie", method = RequestMethod.GET)
    @ApiOperation(value = "根据传入的三部分权重系数进行打分", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "high_flow_weight", value = "高流量用户权重参数", paramType = "query", dataType = "String", defaultValue = "100"),
            @ApiImplicitParam(name = "high_meal_weight", value = "高套餐等级用户权重参数", paramType = "query", dataType = "String", defaultValue = "10"),
            @ApiImplicitParam(name = "pingjing_weight", value = "设备瓶颈权重参数", paramType = "query", dataType = "String", defaultValue = "10")
    })
    public void zhijie_rank(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String high_flow_weight = request.getParameter("high_flow_weight");
        String high_meal_weight = request.getParameter("high_meal_weight");
        String pingjing_weight = request.getParameter("pingjing_weight");


        try{
            rankService.zhijie_rank( high_flow_weight, high_meal_weight, pingjing_weight);
            result.packetResult(true, "直接打分法成功。", null, "0");

        }
        catch (Exception e){

            result.packetResult(false, "直接打分法不成功。获取参数失败或打分逻辑有误", null, "1001");
        }
        finally {
            JsonResult.toJson(result, response);
        }
    }

    /**
     * AHP打分法
     */
    @RequestMapping(value = "/AHP", method = RequestMethod.GET)
    @ApiOperation(value = "根据设定好的凉凉比较矩阵进行打分", response = Result.class)
    public void AHP_rank(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String redirectPath = "http://localhost:8081/rank/AHP_weights";
        System.out.println("redirectPath :"+redirectPath);
        String python_weights_result = "null";
        // 创建httpClient实例对象
        HttpClient httpClient = new HttpClient();
        // 设置httpClient连接主机服务器超时时间：15000毫秒
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
        // 创建post请求方法实例对象
        GetMethod getMethod = new GetMethod(redirectPath);
        // 设置post请求超时时间
        getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
        getMethod.addRequestHeader("Content-Type", "application/json");
        httpClient.executeMethod(getMethod);
        python_weights_result = getMethod.getResponseBodyAsString();
        getMethod.releaseConnection();
        System.out.println(python_weights_result);
        String [] list = python_weights_result.split(",");

        try{
            rankService.AHP_rank(list);
            result.packetResult(true, "AHP打分法成功。", null, "0");
        }
        catch (Exception e){
            result.packetResult(false, "AHP打分法不成功。获取参数失败或打分逻辑有误", null, "1002");
            e.printStackTrace();
        }
        finally {
            JsonResult.toJson(result, response);
        }
    }


    /**
     * 直接打分法
     */
    @RequestMapping(value = "/TOPSIS_pre", method = RequestMethod.GET)
    @ApiOperation(value = "TOPSIS打分法前面操作：数据规范化处理、矩阵归一化处理", response = Result.class)
    public void TOPSIS_rank_pre(HttpServletRequest request, HttpServletResponse response) throws Exception {
        try{
            rankService.TOPSIS_rank_pre_cal();
            result.packetResult(true, "TOPSIS数据规范化处理、矩阵归一化处理成功。", null, "0");
        }
        catch (Exception e){
            result.packetResult(false, "TOPSIS数据规范化处理、矩阵归一化处理不成功。", null, "1001");
        }
        finally {
            JsonResult.toJson(result, response);
        }
    }

    /**
     * TOPSIS打分法
     */
    @RequestMapping(value = "/TOPSIS", method = RequestMethod.GET)
    @ApiOperation(value = "根据判定矩阵进行打分", response = Result.class)
    public void TOPSIS_rank(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String redirectPath = "http://localhost:8081/rank/AHP_TOPSIS_weights";
        System.out.println("redirectPath :"+redirectPath);
        String python_weights_result = "null";
        // 创建httpClient实例对象
        HttpClient httpClient = new HttpClient();
        // 设置httpClient连接主机服务器超时时间：15000毫秒
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
        // 创建post请求方法实例对象
        GetMethod getMethod = new GetMethod(redirectPath);
        // 设置post请求超时时间
        getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
        getMethod.addRequestHeader("Content-Type", "application/json");
        httpClient.executeMethod(getMethod);
        python_weights_result = getMethod.getResponseBodyAsString();
        getMethod.releaseConnection();
        System.out.println(python_weights_result);
        String [] list = python_weights_result.split(",");
        try{
            rankService.TOPSIS_rank(list);
            result.packetResult(true, "TOPSIS打分法成功。", null, "0");
        }
        catch (Exception e){
            result.packetResult(false, "TOPSIS打分法不成功。获取参数失败或打分逻辑有误", null, "1003");
        }
        finally {
            JsonResult.toJson(result, response);
        }
    }

}
